package com.wz.jiangsu.service.serviceImpl;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class TestTaskExecutorServiceImpl {

    public static volatile AtomicInteger i = new AtomicInteger(0);

    /**
     * 无返回结果
     */
    @Async("taskExecutor")
    public void test() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("当前第" + i.incrementAndGet() + "次执行");
    }


    /**
     * 有返回结果
     */
    @Async("taskExecutor")
    public Future<String> test2() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult("当前第" + i.incrementAndGet() + "次执行");
    }
}
